import axios from 'axios';

const request = axios.create({
  timeout: 60000 // 请求超时时间
})

/**
 * 获取地图列表
 */
function getMapList() {
  return request({
    url: '/gs-robot/data/maps',
    method: 'get'
  })
}

/**
 * 获取地图图片
 */
function getMapPng(mapName) {
  return request({
    url: '/gs-robot/data/map_png',
    method: 'get',
    params: {
      map_name: mapName
    }
  })
}
//初始化
function initialize(mapName,positionName) {
  return request({
    url: '/gs-robot/cmd/initialize?map_name='+mapName+'&init_point_name='+positionName,
    method: 'get'
  })
}

//检查初始化
function checkInitialize() {
  return request({
    url: '/gs-robot/cmd/is_initialize_finished',
    method: 'get'
  })
}

/**
 * 停止初始化
 * api文档有误，返回的数据没有data内容.只有msg和successed
 */
function stopInitialize(){
  return request({
    url: '/gs-robot/cmd/stop_initialize',
    method: 'get'
  })
}

/**
 * 显示电量
 */
function showBattery(){
  return request({
    url: '/gs-robot/data/device_status',
    method: 'get'
  })
}

/**
 * 扫描地图
 * @param {String} mapName 地图名称

 */
function scanMap(mapName) {
  return request({
    url: '/gs-robot/cmd/start_scan_map?map_name='+ mapName +'&type=0',
    method: 'get'
  })
}

function cancelScan() {
  return request({
    url: '/gs-robot/cmd/cancel_scan_map',
    method: 'get'
  })
}

function stopScanMap() {
  return request({
    url: 'gs-robot/cmd/async_stop_scan_map',
    method: 'get'
  })
}
/**
 * 返回地图url
 * @param {String} mapName 地图名称
 */
function getMapPngUrl(mapName) {
  return '/gs-robot/data/map_png?map_name=' + mapName
}

/**
 * 返回实时地图
 * 这样写获得的是一张地图，会触发下载的弹窗
 */
function getScanningPng(){
  return request({
    url: '/gs-robot/real_time_data/scan_map_png',
    method: 'get'
  })
}

/**
 * 返回实时地图url
 * 这样写返回的是地图url
 */
function getScanningPngUrl(mapName) {
  return '/gs-robot/real_time_data/scan_map_png?t=' + new Date().getTime();
}

/**
 * 获取某一地图的手绘路径列表
 * 返回结果格式参照API手册
 *
 */
function getDeviceGraphPathsByMapName(mapName) {
  return request({
    url: '/gs-robot/data/graph_paths?map_name=' + mapName,
    method: 'get'
  })
}


/**
 * 获取某一地图的定点列表
 * 返回结果格式参照API手册
 *
 */
function getPositionsByMapName(mapName) {
  return request({
    url: '/gs-robot/data/positions?map_name=' + mapName,
    method: 'get'
  })
}


/**
 * 获取获取实时扫地图图片png
 * 返回结果格式参照API手册
 *
 */
function getScanMap() {
  return request({
    url: '/gs-robot/real_time_data/scan_map_png',
    method: 'get'
  })
}


function insertGraphPath(graphPaths) {
  return request({
    url: '/gs-robot/cmd/generate_graph_path',
    method: 'post',
    data: graphPaths
  })
}

function insertPosition(position){
  return request({
    url:'/gs-robot/cmd/position/add_position',
    method:'POST',
    data:position
  })
}

/**
 * 删除某张地图的手绘图
 * @param mapName
 * @param graphName
 * @returns {AxiosPromise}
 */

function deletePosition( mapName, positionName ) {
  return request({
    url:'/gs-robot/cmd/delete_position?map_name=' + mapName + '&position_name=' + positionName,
    method: 'get'
  })
}

function deleteGraphPath( mapName, graphName ) {
  return request({
    url: '/gs-robot/cmd/delete_graph_path?map_name=' + mapName + '&graph_path_name=' + graphName,
    method: 'get'
  })
}

function renamePosition( mapName,originPosition,newPosition ) {
  return request({
    url: '/gs-robot/cmd/rename_position?map_name='+mapName+'&origin_name=' + originPosition+'&new_name='+newPosition,
    method: 'get'
  })
}


function renameGraphPath( mapName,originGraphPath,newGraphPath) {
  return request({
    url: '/gs-robot/cmd/rename_graph_path?map_name='+mapName+'&origin_name=' + originGraphPath+'&new_name=' + newGraphPath,
    method: 'get'
  })
}


function renameMap( mapName,newMapName ) {
  return request({
    url: '/gs-robot/cmd/rename_map?origin_map_name='+mapName+'&new_map_name=' + newMapName,
    method: 'get'
  })
}

function deleteMap( mapName ) {
  return request({
    url: '/gs-robot/cmd/delete_map?map_name=' + mapName,
    method: 'get'
  })
}

function downloadMap( mapName ) {
  return request({
    url: '/gs-robot/data/download_map?map_name=' + mapName,
    method: 'get'
  })
}

function navigate(positionBean) {
  return request({
    url: '/gs-robot/cmd/navigate',
    method: 'post',
    data:positionBean
  })
}

function startNavigate(mapName,positionName) {
  return request({
    url: '/gs-robot/cmd/position/navigate?map_name='+mapName+'&position_name='+positionName,
    method: 'get'
  })
}


function pauseNavigate() {
  return request({
    url: '/gs-robot/cmd/pause_navigate',
    method: 'get'
  })
}


function resumeNavigate() {
  return request({
    url: '/gs-robot/cmd/resume_navigate',
    method: 'get'
  })
}

function cancelNavigate() {
  return request({
    url: '/gs-robot/cmd/cancel_navigate',
    method: 'get'
  })
}

function getTasksByMapName(mapName) {
  return request({
    url: ' /gs-robot/data/task_queues?map_name='+mapName,
    method: 'get',
   
  })
}

function createTaskBean(mapName,taskName,positionName) {
  let tasks = new Array();
  for(let i =0;i<positionName.length;i++){
      tasks[i] = {
                    "name":"NavigationTask",
                    "start_param":{
                      "map_name":mapName,
                      "position_name":positionName[i]
                    }
                 }
  }
  return {
    "name":taskName, //任务名字
    "map_name":mapName, //　地图名字
    "map_id":"", // 地图id
    "loop":false, // 是否循环执行任务
    "tasks":tasks
  }
}

function createTaskQueue(exexuteQueueSaveBean) {
  return request({
    url: ' /gs-robot/cmd/save_task_queue',
    method: 'post',
    data: exexuteQueueSaveBean
  })
}

function deleteTaskQueue( mapName,taskName ) {
  return request({
    url: ' /gs-robot/cmd/delete_task_queue?map_name=' + mapName + '&task_queue_name='+taskName,
    method: 'get'
  })
}


/**
 * @param mapName
 * @param taskType  这里类型需要看 API 文档填入适当的类型,这里直接创建路径导航
 * @param graphName
 * @param graphPathName
 * @returns {{loop_count: number, loop: boolean, map_name: *, name: string, tasks: {name: *, start_param: {graph_name: *, map_name: *, graph_path_name: *}}[]}}
 */
function createExcuteBean(mapName,taskType,graphName,graphPathName) {
  return {
    "name": "",
    "loop": false,
    "loop_count": 0,
    "map_name": mapName,
    "tasks": [
      {
        "name": taskType,
        "start_param": {
          "map_name": mapName,
          "graph_name": graphName,
          "graph_path_name": graphPathName,
        }
      }
    ]
  }
}

function startTaskGraph(exexuteQueueSaveBean) {
  return request({
    url: '/gs-robot/cmd/start_task_queue',
    method: 'post',
    data: exexuteQueueSaveBean
  })
}

function startTaskQueue(mapName,taskName) {
  return request({
    url: '/gs-robot/cmd/start_task_queue',
    method: 'post',
    data:{
      "name":taskName,
      "loop":false,
      "loop_count":0,
      "map_name":mapName,
      
    }
  })
}

function pauseTask() {
  return request({
    url: '/gs-robot/cmd/pause_task_queue',
    method: 'get'
  })
}

function resumeTask() {
  return request({
    url: '/gs-robot/cmd/resume_task_queue',
    method: 'get'
  })
}

function checkTask() {
  return request({
    url: '/gs-robot/cmd/is_task_queue_finished',
    method: 'get'
  })
}


/**
 * 停止当前任务
 * @returns {AxiosPromise}
 */
function stopCurrentTask() {
  return request({
    url: '/gs-robot/cmd/stop_current_task',
    method: 'get'
  })
}


/**
 * 停止所有队列任务
 */
function stopAllTask() {
  return request({
    url: '/gs-robot/cmd/stop_task_queue',
    method: 'get'
  })
}


/**
 * 创建一个点
 * @param name
 * @param x
 * @param y
 * @returns {{gridPosition: {x: *, y: *}, name: *, actions: Array}}
 */

function createPointBean({name,x,y}) {
  return {
    "actions":[],
    "gridPosition":{
      "x":x,
      "y":y
    },
    "name":name,
  }
}

//创建定点
function createPositionBean({name,mapName,angle,x,y,type}){
  return {
    "angle":angle,
    "gridX":x,
    "gridY":y,
    "mapName":mapName,
    "name":name,
    "type":type
  }
}

function getLocation() {
  return request({
    url: '/gs-robot/real_time_data/position',
    method: 'get'
  })
}

/**
 * 创建一条线
 * @param lineName
 * @param beginName
 * @param endName
 * @param radius
 * @returns {{name: *, end: *, radius: *, begin: *}}
 */
function createLineBean(lineName,beginName,endName,radius){
  return {
    "begin":beginName,
    "end":endName,
    "name":lineName,
    "radius":radius,
  }
}

/**
 * 创建一条手画路径
 * @param name
 * @param lines
 * @param points
 * @returns {{name: *, lines: *, points: *}}
 */
function createPathBean({name,lines,points}){
  return {
    name,
    points,
    lines,
  }
}

/**
 * 创建一个手画图
 * @param mapName
 * @param name
 * @param paths
 * @param lines
 * @param points
 * @returns {{paths: *, name: *, mapName: *, lines: *, points: *}}
 */
function createGraphBean({mapName,name,paths,lines,points}){
  return {
    mapName,
    name,
    paths,
    lines,
    points
  }
}
/**
 * 向前走 
 */
function walkForward(){
  return request({
    url:'/gs-robot/cmd/move',
    method:'post',
    data:{
      "speed":{
        "linearSpeed":1,
        "angularSpeed":0
      }
    }
  })
}
/**
 * 向后走 
 */
function walkBack(){
  return request({
    url:'/gs-robot/cmd/move',
    method:'post',
    data:{
      "speed":{
        "linearSpeed":-1,
        "angularSpeed":0
      }
    }
  })
}
/**
 * 向左转
 */
function turnLeft(){
  return request({
    url: '/gs-robot/cmd/move',
    method: 'post',
    data:{
      "speed":{
        "linearSpeed":0,
        "angularSpeed":1
      }
    }
  })
}
/**
 * 向右转
 */
function turnRight(){
  return request({
    url: '/gs-robot/cmd/move',
    method: 'post',
    data:{
      "speed":{
        "linearSpeed":0,
        "angularSpeed":-1
      }
    }
  })
}
/**
 * 停止转动
 */
function walkStop(){
  return request({
    url: '/gs-robot/cmd/move',
    method: 'post',
    data:{
      "speed":{
        "linearSpeed":0,
        "angularSpeed":0
      }
    }
  })
}

function setSpeedLevel(level){
  return request({
    url: '/gs-robot/cmd/set_speed_level?level='+ level,
    method: 'get',
  })
}

export default {
  // 地图列表
  getMapList,getMapPng,getMapPngUrl, getScanningPngUrl,
  // 扫图相关
  scanMap,cancelScan,stopScanMap,getScanMap,
  // 地图管理
  downloadMap,deleteMap,renameMap,

  // 初始化
  initialize,checkInitialize,stopInitialize,

  // 手画路径
  insertGraphPath, deleteGraphPath,renameGraphPath,getDeviceGraphPathsByMapName,//路径管理
  createPointBean, createLineBean,createPathBean, createGraphBean,//创建路径
  startTaskGraph,//开始路径任务
  // 定点管理
  insertPosition,renamePosition,deletePosition,createPositionBean,getPositionsByMapName,
   //导航相关
   startNavigate,pauseNavigate,resumeNavigate,cancelNavigate,navigate,

  // 任务队列
  getTasksByMapName, createExcuteBean,createTaskBean,createTaskQueue,deleteTaskQueue,//创建和管理队列
  startTaskQueue,pauseTask,resumeTask,stopCurrentTask,stopAllTask,checkTask, //执行队列任务


  // 控制
  showBattery,  getLocation, walkForward,walkBack,turnLeft,turnRight,walkStop,setSpeedLevel
}
